//
//  SettingViewController.m
//  便签
//
//  Created by  on 2022/9/29.
//

#import "SettingViewController.h"
#import "SettingCell.h"
#import "FeedbackViewController.h"
#import "RemindViewController.h"
#define timekey @"timekey"
#define timeSwitch @"timeSwitch"
@interface SettingViewController ()
@property (nonatomic, strong) NSArray *titles;
@property (nonatomic, strong) NSArray *imgs;
@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}
- (void)initUI{
    self.title = LS(@"设置");
    self.titles = @[@"主题颜色",@"提醒",@"反馈",@"隐私政策"];
    self.imgs = @[@"色盘",@"箭头-右",@"箭头-右",@"箭头-右"];
    self.tableViewStyle = UITableViewStyleGrouped;
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.cornerRadius = 12;
    self.tableView.scrollEnabled = NO;
    [self.tableView registerNib:[SettingCell nib] forCellReuseIdentifier:[SettingCell ID]];
    NSLayoutConstraint *top = [self.tableView a1:NSLayoutAttributeTop e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeTop c: 20];
    NSLayoutConstraint *left = [self.tableView a1:NSLayoutAttributeLeft e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeLeft c:20];
    NSLayoutConstraint *right = [self.tableView a1:NSLayoutAttributeRight e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeRight c:-20];
    NSLayoutConstraint *height = [self.tableView a1:NSLayoutAttributeHeight e:NSLayoutRelationEqual t:nil a2:NSLayoutAttributeNotAnAttribute c:200];
    [self.view addConstraints:@[top,left,right,height]];
}

- (void)loadData{
    
}
- (IBAction)actionForBtn:(UIButton *)sender {
    if (sender.tag == 201) {
        APP.user = [User new];
        [self backBtnAction];
    }else if(sender.tag == 202){
        [AlertPopViewController pop:LS(@"注销账号") content:LS(@"您确定要注销账号吗？") type:AlertPopViewControllerTypeCancel block:^(int tag, NSString * _Nonnull text) {
            [UserTool delete:APP.user complete:^(NSDictionary * _Nonnull dict) {
                int code = [dict[@"code"] intValue];
                if (code == 200) {
                    APP.user = [User new];
                    [self backBtnAction];
                }
            }];
        }];
    }
}
- (void)backBtnAction{
    [(MineViewController *)APP.meWindow.rootViewController show];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titles.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SettingCell *cell = [tableView dequeueReusableCellWithIdentifier:[SettingCell ID]];
    cell.title.text = LS(self.titles[indexPath.row]);
    cell.img.image = [UIImage imageNamed:self.imgs[indexPath.row]];
    cell.line.hidden = indexPath.row == self.titles.count - 1;
    return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *title = self.titles[indexPath.row];
    if ([title isEqualToString:@"主题颜色"]) {
        ColorViewController * vc = [ColorViewController new];
        vc.titles[vc.titles.count - 1] = LS(@"默认");
        vc.colors[vc.colors.count - 1] = @"#F2F9F8";
        vc.block = ^(NSString * _Nonnull hexColor) {
            APP.mainColor = hexColor;
            [[NSUserDefaults standardUserDefaults] setValue:hexColor forKey:localMaincolorKey];
            [[NSNotificationCenter defaultCenter] postNotificationName:notifyColorUpdate object:nil];
        };
        [self presentViewController:vc animated:NO completion:nil];
    }else if ([title isEqualToString:@"提醒"]) {
        WS(self)
        RemindViewController *vc = [RemindViewController new];
        NSString *string = [[NSUserDefaults standardUserDefaults] valueForKey:timekey];
        vc.time.text = string == nil || string.length <= 0 ? @"00:00" : string;
        vc.switchBtn.on = [[[NSUserDefaults standardUserDefaults] valueForKey:timeSwitch] boolValue];
        WS(vc)
        vc.block = ^(BOOL switchOn) {
            [[NSUserDefaults standardUserDefaults] setValue:weakvc.time.text forKey:timekey];
            [[NSUserDefaults standardUserDefaults] setValue:@(weakvc.switchBtn.on) forKey:timeSwitch];
            
            PickViewController * pick = [PickViewController new];
            NSMutableArray *hour = [NSMutableArray array];
            NSMutableArray *min = [NSMutableArray array];
            for (int i = 0; i < 24; i++) {
                [hour addObject:[NSString stringWithFormat:@"%01d",i]];
            }
            for (int i = 0; i < 60; i++) {
                [min addObject:[NSString stringWithFormat:@"%01d",i]];
            }
            NSArray *array = @[hour,min];
            pick.array = array;
            pick.block = ^(NSMutableArray *selectindex) {
                NSInteger index1 = [selectindex[0] intValue];
                NSInteger index2 = [selectindex[1] intValue];
                NSString *time = [NSString stringWithFormat:@"%@:%@",hour[index1],min[index2]];
                [[NSUserDefaults standardUserDefaults] setValue:time forKey:timekey];
            };
            [weakself presentViewController:pick animated:NO completion:nil];
        };
        [self presentViewController:vc animated:NO completion:nil];
    }else if ([title isEqualToString:@"反馈"]) {
        FeedbackViewController *vc = [FeedbackViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([title isEqualToString:@"隐私政策"]) {
        PrivateViewController *vc = [PrivateViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return  0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

@end
